/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Control;

import Model.Attribute;
import Model.Concept;
import Model.Correspondence;
import Model.Form;
import Model.FormField;
import Model.FormalConcept;
import Model.Ontology;
import Model.SemanticQualification;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Toinamph
 */
public class SemanticQualificationController {
    
    private static SemanticQualification semanticQualification;
    private static FormalConcept fc;
    private static Correspondence cp; 
    
    public SemanticQualificationController(){
        this.semanticQualification = null;
        this.fc = null;
        this.cp = null;
    }
    
    // semantic qualification
    public static String createSemanticQualificationControl() {
        String ret = null;
        if (semanticQualification == null){
            semanticQualification = new SemanticQualification();
        } else {
            ret = "A semantic qualification is being manipulated !!";
            System.err.println(ret);
        }
        return ret;
    }
    
    public static void setSQ(SemanticQualification sq){
        semanticQualification = sq;
    }
    
    public SemanticQualification getSQ(){
        return this.semanticQualification;
    }
    
    public static void updateSemanticQualificationControl(Form f, List<FormalConcept> fcs){
        if (fcs != null){
            semanticQualification.setFormalConcepts(fcs);
        }
        if (f != null){
            semanticQualification.setFormConcerned(f);
        }
    }
    
    // formal concept
    public static String createFormalConceptControl(String formalConcept_name) {
        String ret = null;
        if(semanticQualification == null) {
            ret = "You cannot create a Formal Concept without creating a Semantic Qualification before";
            System.err.println(ret);
        }
        
        else {
            fc = new FormalConcept(formalConcept_name);
            if (semanticQualification.checkFormalConcept(fc) == -1) {
                ret = "A formal concept with the same name already exists!";
            }
        }
        
        //return fc.getId();
        return ret;
    }
    
    public static String updateListFormalConceptControl(FormalConcept fc){
        String ret = null;
        if ((fc != null)&&(semanticQualification != null)){
            int index = semanticQualification.findFormalConcept(fc.getName());
            if (index != -1){
                semanticQualification.getFormalConcepts().remove(index);
                semanticQualification.getFormalConcepts().add(fc);
            }
        } else {
            ret = "You need to create a formal concept before !";
        }
        return ret;
    }
    
    public static String updateFormalConceptControl(String ont, String cpt){
        String ret = null;
        if (fc != null){
            if (ont != null){
                Ontology myOnt = new Ontology(ont);
                fc.setOntologyAssociated(myOnt);
            } 

            if (cpt != null){
                Concept myCpt = new Concept(cpt);
                fc.setOntologyConcept(myCpt);            
            }
        } else {
            ret = "You need to create a formal concept before !";
        }
        return ret;
    }
    
    public static String deleteFormalConceptControl(FormalConcept myFC){
        String ret = null;
        if (myFC != null && !semanticQualification.getFormalConcepts().isEmpty()){
            semanticQualification.deleteFormalConcept(myFC);
        } else {
            ret = "The formal concept is null or the semantic qualification contains no formal concept !!";
        }
        return ret;
    }
    
    public static String addFormalConceptControl(FormalConcept myFC){
        String ret = null;
        if (myFC != null){
            semanticQualification.addFormalConcept(myFC);
        } else {
            ret = "The formal concept is null !!";
        }
        return ret;
    }
    
    public static FormalConcept getFC (){
        return fc;
    }
    
    public static void setFC (){
        fc = null;
    }
    
    public static void setCP(){
        cp = null;
    }
    
    public static Correspondence getCP(){
        return cp;
    }
    
    public static List<FormalConcept> getFCS(){
        return semanticQualification.getFormalConcepts();
    }
    
    
    public static List<String> FcOntCpt(){
        List<String> ret = new ArrayList<String>();
        List<FormalConcept> fcs = semanticQualification.getFormalConcepts();
        if (!fcs.isEmpty()){
            for (int i=0; i<fcs.size(); i++) {
                String foc = fcs.get(i).getName() + " : " + fcs.get(i).getOntologyAssociated().getName() + " : " + fcs.get(i).getOntologyConcept();
                ret.add(foc);
            }
        } else {
            ret.add("No formal concept has been defined !!");
        }
        return ret;
    }
    
    // correspondance 
    
    public static String createCorrespondenceControl(FormalConcept fc, String att){
        String ret = null;
        if (cp == null){
            Attribute attri = new Attribute(att);
            cp = new Correspondence(fc, attri);
        } else {
            ret = "A correspondence is being manipulated !!";
            System.err.println(ret);
        }
        return ret;
    }
    
    public static String createCorrespondenceControl(String field, FormalConcept fc, String att){
        String ret = null;
        if (cp == null){
            Attribute attri = new Attribute(att);
            FormField ff = new FormField(field);
            cp = new Correspondence(ff, fc, attri);
        } else {
            ret = "A correspondence is being manipulated !!";
            System.err.println(ret);
        }
        return ret;
    }
    
    public static String addCorrespondenceControl(FormalConcept fc, Correspondence cp){
        String ret = null;
        if ((fc != null)&&(cp != null)){
            fc.CreateCorrespondence(cp);
        } else {
            ret = "The formal concept of the correspondence to be added is null !!";
            System.err.println(ret);
        }
        return ret;
    }
    
    public static String deleteCorrespondenceControl(Correspondence myCP){
        String ret = null;
        if ((fc != null)&&(cp != null)){
            fc.RemoveCorrespondence(cp);
        } else {
            ret = "The formal concept of the correspondence to be added is null !!";
            System.err.println(ret);
        }
        return ret;
    }
    
    public static List<String> textCP(FormalConcept myFC){
        List<String> ret = new ArrayList<String>();
        List<Correspondence> cps = myFC.getCorrespondences();
        if (!cps.isEmpty()){
            for (int i=0; i<cps.size(); i++) {
                String cpText = cps.get(i).getField().getNameId() + " : " + cps.get(i).getAttribute();
                ret.add(cpText);
            }
        } else {
            ret.add("No formal concept has been defined !!");
        }
        return ret;
    }
   
    public static void setForm(Form f) {
        semanticQualification.setFormConcerned(f);
    }
    
    public String generateFileContent(){
        String ret = "output file\n";
        ret = ret + "-------------------------------------------------------\n";
        // Header
        // ret = ret + "## Semantic qualification for form : " 
        //          + semanticQualification.getFormConcerned().getName() + "\n";
        //Formal Concepts
        for(FormalConcept fc : semanticQualification.getFormalConcepts()) {
            // we write in the file the COMPLETE name of the ontology concept = OntologyName.ConceptName
            ret = ret + fc.getName() + ":" + fc.getOntologyAssociated().getName() + "." 
                      + fc.getOntologyConcept().getName() +"\n{\n";
            // now write all the correspondences for the formal concept
            for(Correspondence c : fc.getCorrespondences() ) {
                ret = ret + "  " + c.getAttribute().getName() + ":=" + c.getField().getNameId() + ";\n";
            }
            ret = ret + "}\n";
        }
        return ret;
    }
    
    public void writeOutputFile(String content){
        try {
                File file = new File("output.txt");

                // if file doesnt exists, then create it
                if (!file.exists()) {
                        file.createNewFile();
                }

                FileWriter fw = new FileWriter(file.getAbsoluteFile());
                BufferedWriter bw = new BufferedWriter(fw);
                bw.write(content);
                bw.close();

                System.out.println("Done");

        } catch (IOException e) {
                e.printStackTrace();
        }
    }
}
